﻿// MIT License
//
// Copyright (c) 2023-present markeluo

using Admin.NET.Application.Entity.Hr;
using Admin.NET.Application.Service.Hr.Attendance.Dto;
using Admin.NET.Core.Service;
using DingTalk.Api.Response;
using DingTalkAPIHelp.Kaoqin;
using Furion.DatabaseAccessor;
using Furion.FriendlyException;
using System.Collections.Generic;
using System.ComponentModel;
using static DingTalk.Api.Request.OapiAttendanceShiftAddRequest;
using static DingTalk.Api.Response.OapiAttendanceShiftQueryResponse;

namespace Admin.NET.Application.Service.Hr.Attendance;
public class AttendanceShiftService : IDynamicApiController, ITransient
{
    /// <summary>
    /// 获取考勤班次列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("考勤班次列表")]
    [HttpGet("/api/AttendanceShift/list")]
    public async Task<dynamic> list([FromQuery] BasePageInput input)
    { 
        List<TopShiftVoDomain> topShiftVoDomains = new List<TopShiftVoDomain>();

        KaoqinManager manager= new KaoqinManager();
        OapiAttendanceShiftListResponse response = manager.GetShiftList();
        if (response.IsError) return topShiftVoDomains;
        if(response.Result!=null && response.Result.Result.Count>0)
        {
            foreach (var item in response.Result.Result)
            {
               var tmpDtl= manager.GetShiftDetail(item.Id);
                if (tmpDtl!=null && tmpDtl.Success && tmpDtl.Result!=null) {
                    topShiftVoDomains.Add(tmpDtl.Result);
                }
            }
        }
        return topShiftVoDomains; 
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [UnitOfWork]
    [ApiDescriptionSettings(Name = "Add"), HttpPost]
    [DisplayName("增加")]
    public async Task Add(AttendanceShiftInput input)
    {
        TopAtClassVoDomain saveInfo = input.Adapt<TopAtClassVoDomain>();
        for(int i = 0; i < input.Sections.Count; i++)
        {
            saveInfo.Sections[i].Times = new List<TopAtTimeVoDomain>();
            TopAtTimeVoDomain tempTime = null;
            for (int j = 0; j < input.Sections[i].Punches.Count; j++)
            {
                tempTime = input.Sections[i].Punches[j].Adapt<TopAtTimeVoDomain>();
                if (j == 0)
                {
                    tempTime.CheckType = "OnDuty";
                }
                else {
                    tempTime.CheckType = "OffDuty";
                }
                saveInfo.Sections[i].Times.Add(tempTime);
            } 
        }
        saveInfo.Id = null;

        KaoqinManager manager = new KaoqinManager();
        var response= manager.SaveShift(saveInfo);
        if (response.Success) return;
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [UnitOfWork]
    [ApiDescriptionSettings(Name = "Edit"), HttpPost]
    [DisplayName("修改")]
    public async Task Update(AttendanceShiftInput input)
    {
        TopAtClassVoDomain saveInfo = input.Adapt<TopAtClassVoDomain>();
        for (int i = 0; i < input.Sections.Count; i++)
        {
            saveInfo.Sections[i].Times = new List<TopAtTimeVoDomain>();
            TopAtTimeVoDomain tempTime = null;
            for (int j = 0; j < input.Sections[i].Punches.Count; j++)
            {
                tempTime = input.Sections[i].Punches[j].Adapt<TopAtTimeVoDomain>();
                saveInfo.Sections[i].Times.Add(tempTime);
            }
        }
        KaoqinManager manager = new KaoqinManager();
        var delResult= manager.DelShift((long)saveInfo.Id);
        if (delResult.Success)
        {
            saveInfo.Id = null;
            var response = manager.SaveShift(saveInfo);
            if (!response.Success)
                throw new System.Exception("修改班次失败:" + response.ErrMsg);
        }
        else {
            throw new System.Exception("修改班次失败:"+delResult.ErrMsg);
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [UnitOfWork]
    [ApiDescriptionSettings(Name = "Delete"), HttpPost]
    [DisplayName("删除")]
    public async Task Delete(DeleteAttendanceShiftInput input)
    {
        if (input.Id<=0) throw new System.Exception("删除班次失败:主键id为空");
        KaoqinManager manager = new KaoqinManager();
        var delResult = manager.DelShift((long)input.Id);
        if (!delResult.Success)
            throw new System.Exception("删除班次失败:" + delResult.ErrMsg);
    }
}
